<template>
  <div>
    <header id="g-hd">
      <div class="m-topnav">
        <div class="m-topnavbar">
          <span class="tit">服饰馆</span>
          <div class="btns">
            <router-link title="回首页" class="u-topbaridx" to="/home"></router-link>
            <router-link class="u-topbarlogin" to="/login">登录</router-link>
          </div>
        </div>
      </div>
    </header>
    <div class="nav__content">
      <div class="topbar-filter" :class="fixed?'fixed':''">
        <ul class="topbar-filter--type">
          <router-link to="/list/multiple" tag="li" class="topbar-filter--li">
            <span class="topbar-filter--sortname">综合</span>
          </router-link>
          <router-link to="/list/sales" tag="li" class="topbar-filter--li">
            <span class="topbar-filter--sortname">销量</span>
          </router-link>

          <router-link to="/list/price" tag="li" class="topbar-filter--li">
            <span class="topbar-filter--sortname">价格</span>
            <span class="topbar-filter--arrow">
              <em class="up" @click="ZsortBtn()"></em>
              <em class="down" @click="sortBtn()"></em>
            </span>
          </router-link>

          <router-link to="/list/new" tag="li" class="topbar-filter--li">
            <span class="topbar-filter--sortname">新品</span>
          </router-link>
        </ul>
      </div>
    </div>
    <router-view></router-view>
  </div>
</template>

<script>
import Vue from "vue";
import VueLazyload from "vue-lazyload";
Vue.use(VueLazyload, {
  // 加载时要显示的图片
  loading:
    "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1600509675204&di=7d84260dd3fafd89e090763c7aadfc0b&imgtype=0&src=http%3A%2F%2Fimage.biaobaiju.com%2Fuploads%2F20191101%2F15%2F1572591694-MuDWBKSJQs.jpg",
});
export default {
  data() {
    return {
      fixed: false,
      list: [],
    };
  },
  created() {
    // 发布一个让底部菜单隐藏的指令
    this.$bus.$emit("footernav", false);
  },
  methods: {
    sortBtn() {
      //  console.log("sort")
      this.list.sort(this.sortMethods("price"));
    },
    ZsortBtn() {
      this.list.sort(this.ZsortMethods("price"));
    },
    sortMethods(property) {
      return function (a, b) {
        var value1 = a[property];
        var value2 = b[property];
        return value2 - value1;
      };
    },
    ZsortMethods(property) {
      return function (a, b) {
        var value1 = a[property];
        var value2 = b[property];
        return value1 - value2;
      };
    },
  },
  mounted() {
    window.addEventListener(
      "scroll",
      (e) => {
        let top = document.documentElement.scrollTop;
        //console.log(top)
        if (top > 85) {
          //console.log("吸顶")
          this.fixed = true;
        } else {
          //console.log("还原")
          this.fixed = false;
        }
      },
      true
    );

    this.$bus.$on("sortBtn", (data) => {
      this.list = data;
      //console.log(this.list)
    });
  },
  // 组件注销前执行方法
  beforeDestroy() {
    // 发布一个让底部菜单显示的指令
    this.$bus.$emit("footernav", true);
  },
};
</script>

<style lang="scss" scoped>
* {
  margin: 0;
  padding: 0;
  outline: none;
}
.fixed {
  position: fixed;
  top: 0;
}

#g-hd {
  display: block;
  box-sizing: border-box;
}
.m-topnav {
  position: relative;
  line-height: 45px;
  height: 45px;
  text-align: center;
  background-color: #fff;
  overflow: hidden;
  transition: height 0.4s;
  border-bottom: 1px solid #f0f0f0;
  .m-topnavbar {
    height: 45px;
    line-height: 45px;
    text-align: center;
    .tit {
      font-size: 18px;
      overflow: hidden;
      display: inline-block;
      white-space: nowrap;
      text-overflow: ellipsis;
      max-width: 52%;
    }
    .btns {
      position: absolute;
      right: 15px;
      top: 50%;
      margin-top: -12px;
      height: 30px;
      line-height: 30px;
      color: #e31436;
      .u-topbaridx {
        margin-left: 10px;
        vertical-align: top;
        color: inherit;
        background-image: url();
        width: 22px;
        height: 22px;
        display: inline-block;
        background-size: 22px;
        margin-top: 1px;
      }
      .u-topbarlogin {
        margin-left: 10px;
        vertical-align: top;
        color: inherit;
        display: inline-block;
        margin-top: 1px;
        width: 36px;
        height: 10px;
        line-height: 10px;
        padding: 5px 0;
        text-align: center;
        border: 1px solid;
        border-radius: 3px;
        font-size: 10px;
      }
    }
  }
}

.nav__content {
  min-height: 100%;
  background-color: #fff;
  .topbar-filter {
    height: 40px;
    border-top: none;
    border-width: 0.5px;
    background: #fff;
    // border-top: 1px solid #e5e5e5;
    border-bottom: 1px solid #e5e5e5;
    width: 100%;
    color: #666;
    z-index: 10;
    transition: all 0.4s;
    .topbar-filter--type {
      zoom: 1;
      display: flex;
      color: #666;
      .topbar-filter--type:after {
        display: block;
        clear: both;
        visibility: hidden;
        height: 0;
        overflow: hidden;
        content: ".";
      }
      .topbar-filter--li {
        flex: 1;
        text-align: center;
        line-height: 38px;
        font-size: 14px;
        list-style: none;
        display: list-item;
        .topbar-filter--sortname {
          display: inline-block;
          vertical-align: middle;
        }
      }
    }
  }
}

.topbar-filter--arrow {
  display: inline-block;
  position: relative;
  top: 0;
  left: 0;
  width: 6px;
  height: 10px;
  vertical-align: middle;
  box-sizing: border-box;
}
.topbar-filter--arrow .up {
  position: absolute;
  top: 0;
  left: 0;
  width: 0;
  height: 0;
  border-bottom: 4px solid #999;
  border-left: 3px solid transparent;
  border-right: 3px solid transparent;
  border-top: 0 solid transparent;
  font-weight: 400;
  font-style: normal;
}

.topbar-filter--arrow .down {
  position: absolute;
  top: 6px;
  left: 0;
  width: 0;
  height: 0;
  border-top: 4px solid #999;
  border-left: 3px solid transparent;
  border-right: 3px solid transparent;
  border-bottom: 0 solid transparent;
  font-weight: 400;
  font-style: normal;
}
.router-link-active {
  color: #e31436;
  cursor: pointer;
}
</style>
